/**
 * 微信工具类
 */
var WeChatUtil = {};
/**
 * 是否安装
 * @return {Boolean}
 * @public
 */
WeChatUtil.isInstalled = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('WeChatUtil', 'isInstalled');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/WeChatUtil', 'isInstalled', '()Z');
    }
};
/**
 * 拉起登录
 * @public
 */
WeChatUtil.redirectLogin = function (key, data) {
    WeChatUtil.key = key;
    WeChatUtil.data = data;
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('WeChatUtil', 'redirectLogin');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/WeChatUtil', 'redirectLogin', '()V');
    }
};
/**
 * 微信登录回调
 * @param {String} code
 * @public
 */
WeChatUtil.loginResq = function (code) {
    eventManager.dispatchEvent(EventKey.ReceiveWechatCode, {code: code, key: WeChatUtil.key, data: WeChatUtil.data});
    WeChatUtil.key = null;
    WeChatUtil.data = null;
};
/**
 * 微信分享回调
 * @param code
 * @public
 */
WeChatUtil.shareResq = function (code) {
    eventManager.dispatchEvent(EventKey.ReceiveWechatShare, code);
};
/**
 * 支付回调
 * @param code
 */
WeChatUtil.payResq = function (code) {

};
/**
 * 分享网址
 * @param {String} url
 * @param {String} title
 * @param {String} description
 * @param {Number} sceneType 分享场景
 * @public
 */
WeChatUtil.shareUrl = function (url, title, description, sceneType) {
    sceneType = sceneType || WeChatUtil.SceneType.Friend;
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'shareUrl:title:description:sceneType:',
            url, title, description, sceneType
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'shareUrl', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V',
            url, title, description, sceneType
        );
    }
};
/**
 * 分享网址
 * @param {String} url
 * @param {String} title
 * @param {String} description
 * @param {String} path
 * @param {Number} [sceneType] 分享场景
 * @public
 */
WeChatUtil.shareUrlWithIcon = function (url, title, description, path, sceneType) {
    sceneType = sceneType || WeChatUtil.SceneType.Friend;
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'shareUrlWithIcon:title:description:path:sceneType:',
            url, title, description, path, sceneType
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'shareUrlWithIcon', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V',
            url, title, description, path, sceneType
        );
    }
};
/**
 * 分享图片
 * @param {String} path
 * @param {Number} [sceneType]
 * @public
 */
WeChatUtil.sharePic = function (path, sceneType) {
    sceneType = sceneType || WeChatUtil.SceneType.Friend;
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'sharePic:sceneType:',
            path, sceneType
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'sharePic', '(Ljava/lang/String;I)V',
            path, sceneType
        );
    }
};
/**
 * 截图
 * @param {cc.Node} node
 * @param {Number} [sceneType]
 * @public
 */
WeChatUtil.shareCapture = function (node, sceneType) {
    sceneType = sceneType || WeChatUtil.SceneType.Friend;
    captureNode(node, function (path) {
        WeChatUtil.sharePic(path, sceneType);
    });
};
/**
 * 分享文本
 * @param {String} text
 * @param {Number} [sceneType]
 */
WeChatUtil.shareText = function (text, sceneType) {
    sceneType = sceneType || WeChatUtil.SceneType.Friend;
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'shareText:sceneType:',
            text, sceneType
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'shareText', '(Ljava/lang/String;I)V',
            text, sceneType
        );
    }
};
/**
 * 微信分享小程序
 * @param url
 * @param userName
 * @param path
 * @param hdImagePath
 * @param title
 * @param description
 */
WeChatUtil.shareSmallAppWithIcon = function (url, userName, path, hdImagePath, title, description) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'shareSmallAppWithIcon:userName:path:hdImagePath:title:description:',
            url, userName, path, hdImagePath, title, description
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'shareSmallAppWithIcon', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V',
            url, userName, path, hdImagePath, title, description
        );
    }
};
/**
 * 微信订阅
 * @param scene
 * @param template_id
 * @param reserved
 */
WeChatUtil.subscription = function (scene, template_id, reserved) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'WeChatUtil',
            'subscription:template_id:reserved:',
            scene, template_id, reserved
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            'subscription', '(ILjava/lang/String;Ljava/lang/String;)V',
            scene, template_id, reserved
        );
    }
};

/**
 * 微信拉起网页支付
 * @param cid
 */
WeChatUtil.beginWxPay = function (cid) {
    cid = cid || 1;
    var signKey = Crypto.MD5("feiyu-pay" + gameData.uid + app.area);
    cc.sys.openURL("http://"+app.payUrl+"/payServer/wxpay/unify_pay/index.html?area=" + app.area + "&buyerId=" + gameData.uid + "&source=2&ciType=" + cid + "&signKey=" + signKey);
};

/**
 * 场景定义
 * @public
 */
WeChatUtil.SceneType = {
    Friend: 0,
    Round: 1
};

/**
 * 分享小程序
 * @param {String} url
 * @param {String} title
 * @param {String} description
 * @param {String} texture
 * @param {String} miniId
 * @param {String} path
 * */
WeChatUtil.shareMiniPro = function (url, title, description, texture, miniId, path) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            "WeChatUtil",
            "shareMiniPro:title:description:hdImagePath:userName:path:miniProgramType:",
            url,
            title,
            description,
            texture,
            miniId,
            path,
            2 // 正式版:0，测试版:1，体验版:2
        );
    }
    else {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            "shareMiniPro",
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
            url,
            title,
            description,
            texture,
            miniId,
            path,
            "2" // 正式版:0，测试版:1，体验版:2
        );
    }
};

WeChatUtil.setMiniProRoomId = function (roomid) {
    return appId = (cc.sys.os == cc.sys.OS_IOS
        ? jsb.reflection.callStaticMethod("WeChatUtil", "setminiRroRoomId:", roomid)
        : jsb.reflection.callStaticMethod(packageUri + '/utils/WeChatUtil', "setminiRroRoomId", "(Ljava/lang/String;)V", roomid));
};
WeChatUtil.getMiniProRoomId = function () {
    var roomID = "";
    if (cc.sys.os == cc.sys.OS_IOS) {
        roomID = jsb.reflection.callStaticMethod(
            "WeChatUtil",
            "getminiRroRoomId")
    }
    else {
        roomID = jsb.reflection.callStaticMethod(
            packageUri + '/utils/WeChatUtil',
            "getminiRroRoomId",
            "()Ljava/lang/String;"
        )
    }
    return roomID;
};